<?php
/**
 * 发送短信验证码
 * @param string $phone
 * @param int $code
 **/
function sendSMSMessage($phone, $code, $name='', $type='',$room_number='', $times='', $istype=0){
	$smsurl = 'http://m.5c.com.cn/api/send/index.php?';
	if($istype == 1){
		$message = "尊敬的嘛嘛公寓". $room_number ."房间". $name ."客人，你好！您的房间费用将于". $times ."到期，请及时缴纳下期费用。管家电话：(023)65157522.";
	}else if($istype == 2){
		$message = "尊敬的嘛嘛公寓". $room_number ."房间". $name ."客人，你好！您的合同将于". $times ."到期，请及时续约。管家电话：(023)65157522.";
	}else{
		$message = "【嘛嘛公寓】尊敬的".$room_number."房间 ".$name." 客人，您好！欢迎你入住嘛嘛城市公寓熙街店。您的授权码为：". $code ."。可以前台自助制卡，关注微信公众号<嘛嘛公寓>，可查水电费。公寓管家热线：023-65659672。嘛嘛公寓，有爱的青年创意公寓！";
	}
	
	$param = array(
		'username' => 'mmgy',
		'password_md5' => md5('mamagongyu2015'),
		'apikey' => 'cd983820e908517d1c8b6fb9762a5ae3',
		'mobile' => $phone,
		'content' => urlencode($message),
		'encode' => 'UTF-8'
	);
	
	$paramstring = http_build_query($param);
	$content = file_get_contents($smsurl. $paramstring);
	if(strpos($content, 'success') >= 0){
		return true;
	}else{
		return false;
	}
}

/**
 * 发送预约用户注册短信验证码
 * @param string $phone
 * @param int $code
 **/
function sendYuYueSMSMessage($phone, $code){
	$smsurl = 'http://m.5c.com.cn/api/send/index.php?';
	
	$message = "欢迎注册嘛嘛公寓会员，您的注册验证码为：【". $code ."】";

	$param = array(
		'username' => 'mmgy',
		'password_md5' => md5('mamagongyu2015'),
		'apikey' => 'cd983820e908517d1c8b6fb9762a5ae3',
		'mobile' => $phone,
		'content' => urlencode($message),
		'encode' => 'UTF-8'
	);
	
	$paramstring = http_build_query($param);
	$content = file_get_contents($smsurl. $paramstring);
	if(strpos($content, 'success') >= 0){
		return true;
	}else{
		return false;
	}
}

/**
 * 计算月数
 **/
function getMonthNum($date1, $date2, $tags='-' ){
	list($dateArray1['y'], $dateArray1['m'], $dateArray1['d'])=explode("-", $date1);
	list($dateArray2['y'], $dateArray2['m'], $dateArray2['d'])=explode("-", $date2);
	//1. 计算相差的年份
	$year = abs($dateArray1['y'] - $dateArray2['y']);
	//2. 计算月份
	$month = $year * 12;
	//3. 判断相差的月份
	$month = $month + $dateArray2['m'] - $dateArray1['m'];
	//4. 计算天数
	$day = abs($dateArray1['d'] - $dateArray2['d']);
	if($day > 5){
		$month = $month + 1;
	}else{
		$month = $month;
	}
	
	return $month;
}

function getMonthNum2($date1, $date2, $tags='-' ){
	$date1_stamp=strtotime($date1);
	$date2_stamp=strtotime($date2);
	list($date_1['y'],$date_1['m'],$date_1['d'])=explode("-",date('Y-m-d',$date1_stamp));
	list($date_2['y'],$date_2['m'],$date_2['d'])=explode("-",date('Y-m-d',$date2_stamp));
	//获取月份
	$month = abs($date_1['y']-$date_2['y'])*12 +$date_2['m']-$date_1['m'];
	return $month;
}

/**
 * 重写strtotime()方法
 *
 * @param int $time 开始时间戳
 * @param int $month 月数
 * @param string $type +/-
 * @param int $isac 是否减去1天; true: -1天 false: +1天
 *
 * @return int 新的时间戳
 **/
function c_strtotime($time, $month, $type, $isac=true){
	//得到时间
	$timeFormt = date('Y-m-01', $time);
	//获取开始日期的day
	$beginDay = date('d', $time);
	//判断开始时间月的总天数
	$newTimeFormt = date('Y-m', strtotime($type. "". $month . " month", strtotime($timeFormt)));
	//获取当月的总天数
	$maxDay = date('t', strtotime($newTimeFormt.'-01'));
	//判断是否是2月
	if(date('m', $time) == 2){
		$maxDay2 = date('t', strtotime($timeFormt));
		if($maxDay2 - $beginDay <= 1){
			if($isac){
				$beginDay = $maxDay;
			}else{
				$beginDay = $maxDay - 1;
			}
		}
	}
	
	//是否减去一天
	if($isac){
		$beginDay = $beginDay - 1;
	}
	
	if(!$beginDay){
		return strtotime("-1 day", strtotime($newTimeFormt.'-01'));
	}
	//获取当月的总天数
	if($maxDay < $beginDay){
		$beginDay = $maxDay;
	}
	
	return strtotime($newTimeFormt.'-'. str_pad($beginDay, 2, '0', STR_PAD_LEFT));
}

function returnMessage($errorCode, $content){
	$message = array(
		'errCode' => $errorCode,
		'content' => $content
	);

	return json_encode($message);
}

function getAgeByID($id){  
	//过了这年的生日才算多了1周岁 
    if(empty($id)) return ''; 
    $date=strtotime(substr($id,6,8));
	//获得出生年月日的时间戳 
    $today=strtotime('today');
	//获得今日的时间戳 111cn.net
    $diff=floor(($today-$date)/86400/365);
	//得到两个日期相差的大体年数 
	//strtotime加上这个年数后得到那日的时间戳后与今日的时间戳相比 
    $age=strtotime(substr($id,6,8).' +'.$diff.'years')>$today?($diff+1):$diff; 

    return $age; 
} 
